(function(document, undefined){
google.load('jquery', '1.4.2');
google.load('friendconnect', '0.8', {'base_domain': 'www.google.com'});

gfcCounter = window.gfcCounter || 0;

Login = window.Login || (function(){
  
  var popupLogging = function() {
    $('<div id="overlay"></div>').appendTo($('body'));
    $('<div id="login-dialog"><img src="/img/spinner.gif" width="32" height="32" alt=""> 登入進行中...</p></div>').css({
      'left': ($(window).width() - 360) / 2 + 'px',
      'top': ($(window).height() - 80) / 2 + 'px'
    }).appendTo($('body'));
  };
  
  var initGFC = function() {
    google.friendconnect.container.setParentUrl('/');
    google.friendconnect.container.initOpenSocialApi({
      site: '12342157770461837616',
      onload: function(st) {
        gfcCounter += 1;
        if (gfcCounter > 1) {
          window.top.location.href = '/user/auth?user_type=gfc';
        }
      }
    });
    google.friendconnect.renderSignInButton({
      "id" : "login-fcbutton",
      "text" : "使用 Google Friend Connect",
      "style" : "long"
    });
    
    $('.gfc-button-2').live('click', function(e) {
      popupLogging();
    });
  };
  
  var initFB = function() {
    FB.init({
      appId: '139572806069901',
      status: true,
      cookie: true,
      xfbml: true
    });
    
    $('#fb-login').click(function(e){
      popupLogging();
      FB.login(function(response) {
        if (response.status == 'connected') {
          window.location.href = '/user/auth?user_type=fb'
        }
      });
      return false;
    });
  };
  
  return {
    init: function() {
      initGFC();
      initFB();
    }
  };
  
})();

google.setOnLoadCallback(function(){
  Login.init();
});
  
})(document);
